package com.sixbro.caicaitv.storage;

import android.content.Context;
import android.content.SharedPreferences;

import com.sixbro.caicaitv.BaseAppContext;


public class SPHelper {
    public static volatile SharedPreferences mSharedPreferences;

    //设置SharedPreferences
    public static void set(String key, int value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public static void set(String key, boolean value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public static void set(String key, String value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static void set(String key, long value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putLong(key, value);
        editor.apply();
    }

    public static boolean get(String key, boolean defValue) {
        return getPreferences().getBoolean(key, defValue);
    }

    public static String get(String key, String defValue) {
        return getPreferences().getString(key, defValue);
    }

    public static int get(String key, int defValue) {
        return getPreferences().getInt(key, defValue);
    }

    public static long get(String key, long defValue) {
        return getPreferences().getLong(key, defValue);
    }

    public static float get(String key, float defValue) {
        return getPreferences().getFloat(key, defValue);
    }

    public static SharedPreferences getPreferences() {
        if (mSharedPreferences == null) {
            synchronized (SPHelper.class) {
                if (mSharedPreferences == null) {
                    return BaseAppContext.getContext().getSharedPreferences("sp_storage", Context.MODE_PRIVATE);
                }
            }
        }
        return mSharedPreferences;
    }

}
